package com.simple.util;

import java.io.UnsupportedEncodingException;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import com.simple.util.common.SpringUtil;
import com.simple.util.common.StringUtil;
import com.simple.util.exception.SimpleSystemException;

public class EmailUtil {
	public static void sendHtmlEmail(final String fromMail,
			final String toMail, final String ccMail, final String msgSubject,
			final String msgBody) throws MessagingException {
		sendEmail(fromMail, toMail, ccMail, msgSubject, msgBody, true);
	}

	public static void sendEmail(final String fromMail, final String toMail,
			final String ccMail, final String msgSubject, final String msgBody)
			throws MessagingException {
		sendEmail(fromMail, toMail, ccMail, msgSubject, msgBody, false);
	}

	public static void sendHtmlEmail(final String fromMail,
			final String[] toMails, final String[] ccMails,
			final String msgSubject, final String msgBody)
			throws MessagingException {
		sendEmail(fromMail, toMails, ccMails, msgSubject, msgBody, true);
	}

	public static void sendEmail(final String fromMail, final String[] toMails,
			final String[] ccMails, final String msgSubject,
			final String msgBody) throws MessagingException {
		sendEmail(fromMail, toMails, ccMails, msgSubject, msgBody, false);
	}

	private static void sendEmail(final String fromMail, final String toMail,
			final String ccMail, final String msgSubject, final String msgBody,
			final boolean htmlFlag) throws MessagingException {
		final JavaMailSenderImpl mailSender = (JavaMailSenderImpl) SpringUtil
				.getBean("mailSender");

		final MimeMessage mimeMessage = mailSender.createMimeMessage();
		final MimeMessageHelper messageHelper = new MimeMessageHelper(
				mimeMessage, false);

		final InternetAddress internetAddress = translateEmailAddress(fromMail);
		messageHelper.setFrom(internetAddress);
		messageHelper.setTo(toMail);
		if (!StringUtil.isNull(ccMail)) {
			messageHelper.setCc(ccMail);
		}
		messageHelper.setSubject(msgSubject);
		messageHelper.setText(msgBody, htmlFlag);
		mailSender.send(mimeMessage);
	}

	private static void sendEmail(final String fromMail,
			final String[] toMails, final String[] ccMails,
			final String msgSubject, final String msgBody,
			final boolean htmlFlag) throws MessagingException {
		final JavaMailSenderImpl mailSender = (JavaMailSenderImpl) SpringUtil
				.getBean("mailSender");

		final MimeMessage mimeMessage = mailSender.createMimeMessage();
		final MimeMessageHelper messageHelper = new MimeMessageHelper(
				mimeMessage, false);

		final InternetAddress internetAddress = translateEmailAddress(fromMail);
		messageHelper.setFrom(internetAddress);
		messageHelper.setTo(toMails);
		if (ccMails.length > 0) {
			messageHelper.setCc(ccMails);
		}
		messageHelper.setSubject(msgSubject);
		messageHelper.setText(msgBody, htmlFlag);
		mailSender.send(mimeMessage);
	}

	/**
	 * translate EmailAddress to InternetAddress
	 * 
	 * @param emailAddress
	 *            : such as a@b.com or a@b.com(name)
	 * @return
	 */
	private static InternetAddress translateEmailAddress(
			final String emailAddress) {
		final InternetAddress internetAddress = new InternetAddress();
		try {
			final int pos = emailAddress.indexOf('(');
			if (pos < 0) {
				internetAddress.setAddress(emailAddress);
			} else {
				if (pos > 0) {
					internetAddress.setAddress(emailAddress.substring(0, pos));
					final String name = emailAddress.substring(pos + 1);
					if (name.lastIndexOf(')') == name.length() - 1
							&& name.length() > 1) {
						internetAddress.setPersonal(
								name.substring(0, name.length() - 1), "UTF-8");
					}
				} else {
					internetAddress.setAddress("");
				}
			}
		} catch (UnsupportedEncodingException e) {
			throw new SimpleSystemException("message.system.error", e);
		}
		return internetAddress;
	}
}
